package service

import (
	"github.com/gin-gonic/gin"
	"github.com/astaxie/beego/orm"
	"talentsystem/model"
)
//求职者所有信息
func GetAccountApplyJob(c*gin.Context){
	var param model.GetAccountApplyJob
	if err:=c.Bind(&param);err!=nil{
		c.JSON(500,gin.H{
			"status":500,
			"msg":"参数格式不正确",
		})
		return
	}
	offset:=(param.Page-1)*param.Pagesize
	o:=orm.NewOrm()
	var msg []model.AccountApplyJob
	//_,err:=o.QueryTable("account_apply_job").Offset(offset).Limit(param.Pagesize).All(&msg,"id","phone","user_name","sex","age","fphone","fid","pic_url","time")
	query:=o.QueryTable("account_apply_job").Offset(offset).Limit(param.Pagesize)
	if param.Id!=0 {
		query=query.Filter("id",param.Id)
	}
	if param.Phone!=""{
		query=query.Filter("phone",param.Phone)
	}
	num,err:=query.OrderBy("-id").All(&msg,"id","phone","user_name","sex","age","fphone","fid","pic_url","commission","time")
	if err!= nil{
		c.JSON(500, gin.H{
			"status": 500,
			"msg":    "系统出错了,请刷新",
		})
		return
	}
/*	num,er:=o.QueryTable("account_apply_job").Count()
	if er!= nil{
		c.JSON(500, gin.H{
			"status": 500,
			"msg":    "系统出错了,请刷新",
		})
		return
	}*/
	c.JSON(200, gin.H{
		"status": 200,
		"total":  num,
		"data":msg,
	})
	return
}
//一个求职者的信息
func GetOneAccountApplyJob(c*gin.Context){
	var param model.TypeId
	if err:=c.Bind(&param);err!=nil{
		c.JSON(500,gin.H{
			"status":500,
			"msg":"参数格式不正确",
		})
		return
	}
	o:=orm.NewOrm()
	var msg []model.AccountApplyJob
	err:=o.QueryTable("account_apply_job").Filter("id",param.Id).One(&msg,"id","phone","user_name","sex","age","fphone","fid","pic_url","time")
	if err!= nil{
		c.JSON(500, gin.H{
			"status": 500,
			"msg":    "系统出错了,请刷新",
		})
		return
	}
	c.JSON(200, gin.H{
		"status": 200,
		"data":msg,
	})
	return
}
//修改求职者信息
func UpdateAccountApplyJob(c *gin.Context){
	var param model.UpdateAccountApplyJob
	if err:=c.Bind(&param);err!=nil{
		c.JSON(500,gin.H{
			"status":500,
			"msg":"参数格式不正确",
		})
		return
	}
	o:=orm.NewOrm()
	_,err:=o.QueryTable("account_apply_job").Filter("id",param.Id).Update(orm.Params{
		 "UserName":param.UserName,
		 "Sex":param.Sex,
		 "Age":param.Age,
	})
	if err!= nil{
		c.JSON(500, gin.H{
			"status": 500,
			"msg":    "系统错误,请刷新重试",
		})
		return
	}
	c.JSON(200, gin.H{
		"status": 200,
		"msg":"修改成功",
	})
	return
}
//插入求职者信息
func InsertAccountApplyJob(c*gin.Context){
	var param model.InsertAccountApplyJob
	if err:=c.Bind(&param);err!=nil{
		c.JSON(500,gin.H{
			"status":500,
			"msg":"参数格式不正确",
		})
		return
	}
	total,e:=orm.NewOrm().QueryTable("account_apply_job").Filter("phone",param.Phone).Count()
	if e!=nil{
		c.JSON(500, gin.H{
			"status": 500,
			"msg":    "系统出错了,请刷新",
		})
		return
	}
	if total>0{
		c.JSON(200, gin.H{
			"status": 200,
			"msg":    "该手机号已被注册",
		})
		return
	}
	o:=orm.NewOrm()
	accountapplyjob:= new(model.AccountApplyJob)
	accountapplyjob.Phone =param.Phone
	accountapplyjob.PassWord=param.PassWord
	accountapplyjob.UserName =param.UserName
	accountapplyjob.Sex =param.Sex
	accountapplyjob.Age=param.Age
	accountapplyjob.Fphone=param.Fphone
	accountapplyjob.Fid=param.Fid
	accountapplyjob.Wxopenid=param.Wxopenid
	accountapplyjob.Time=param.Time
	num,err:=o.Insert(accountapplyjob)
	if err!= nil{
		c.JSON(500, gin.H{
			"status": 500,
			"msg":    "系统错误,请刷新重试",
		})
		return
	}
	c.JSON(200, gin.H{
		"status": 200,
		"msg":"添加成功",
		"thisid":num,
	})
	return
}
//猎头所有信息
func GetAccountHeadhunters(c*gin.Context){
	var param model.GetAccountApplyJob
	if err:=c.Bind(&param);err!=nil{
		c.JSON(500,gin.H{
			"status":500,
			"msg":"参数格式不正确",
		})
		return
	}
	offset:=(param.Page-1)*param.Pagesize
	o:=orm.NewOrm()
	var msg []model.AccountHeadhunters
	//num,err:=o.QueryTable("account_headhunters").Offset(limit).Limit(param.Pagesize).All(&msg,"id","phone","user_name","sex","age","fphone","fid","pic_url","time")
	query:=o.QueryTable("account_headhunters").Offset(offset).Limit(param.Pagesize)
	if param.Id!=0 {
		query=query.Filter("id",param.Id)
	}
	if param.Phone!=""{
		query=query.Filter("phone",param.Phone)
	}
	num,err:=query.OrderBy("-id").All(&msg,"id","phone","user_name","sex","age","fphone","fid","pic_url","commission","head_name","time")
	if err!= nil{
		c.JSON(500, gin.H{
			"status": 500,
			"msg":    "系统出错了,请刷新",
		})
		return
	}
	if err!= nil{
		c.JSON(500, gin.H{
			"status": 500,
			"msg":    "系统错误,请刷新重试",
		})
		return
	}
	/*num,er:=o.QueryTable("account_headhunters").Count()
	if er!= nil{
		c.JSON(500, gin.H{
			"status": 500,
			"msg":    "系统出错了,请刷新",
		})
		return
	}*/
	c.JSON(200, gin.H{
		"status": 200,
		"total":  num,
		"data":msg,
	})
	return
}
//拿到一个猎头的信息
func GetOneAccountHeadhunters(c*gin.Context){
	var param model.TypeId
	if err:=c.Bind(&param);err!=nil{
		c.JSON(500,gin.H{
			"status":500,
			"msg":"参数格式不正确",
		})
		return
	}
	o:=orm.NewOrm()
	var msg []model.AccountHeadhunters
	err:=o.QueryTable("account_headhunters").Filter("id",param.Id).One(&msg,"id","phone","user_name","sex","age","fphone","fid","pic_url","time","head_name","commission")
	if err!= nil{
		c.JSON(500, gin.H{
			"status": 500,
			"msg":    "系统出错了,请刷新",
		})
		return
	}
	c.JSON(200, gin.H{
		"status": 200,
		"data":msg,
	})
	return
}
//修改猎头信息
func UpdateAccountHeadhunters(c *gin.Context){
	var param model.UpdateAccountApplyJob
	if err:=c.Bind(&param);err!=nil{
		c.JSON(500,gin.H{
			"status":500,
			"msg":"参数格式不正确",
		})
		return
	}
	o:=orm.NewOrm()
	_,err:=o.QueryTable("account_headhunters").Filter("id",param.Id).Update(orm.Params{
		"UserName":param.UserName,
		"Sex":param.Sex,
		"Age":param.Age,
		"HeadName":param.HeadName,
		"Commission":param.Commission,
	})
	if err!= nil{
		c.JSON(500, gin.H{
			"status": 500,
			"msg":    "系统错误,请刷新重试",
		})
		return
	}
/*	if param.Fphone!=""||param.Fid!=0{
		var result model.AccountHeadhunters
		err:=orm.NewOrm().Raw("SELECT id FROM account_headhunters WHERE id=? OR phone=?",param.Fid,param.Phone).QueryRow(&result)
		if err!= nil{
			c.JSON(500, gin.H{
				"status": 500,
				"msg":    "系统错误,请刷新重试",
			})
			return
		}
		if result.Id!=0{
			total,err:=orm.NewOrm().QueryTable("grade").Filter("account_id",result.Id).Count()
			if err!= nil{
				c.JSON(500, gin.H{
					"status": 500,
					"msg":    "系统错误,请刷新重试",
				})
				return
			}
			if total>0{
				var grade model.Grade
				err=orm.NewOrm().QueryTable("grade").Filter("account_id",result.Id).One(&grade)
				if err!= nil{
					c.JSON(500, gin.H{
						"status": 500,
						"msg":    "系统错误,请刷新重试",
					})
					return
				}

					num,err:=orm.NewOrm().QueryTable("grade").Filter("account_id",param.Id).Update(orm.Params{
						"TeamId":result.Id,
						"GroupId":grade.TeamId,
					})


			}else if total==0{
				_,err:=orm.NewOrm().QueryTable("grade").Filter("account_id",param.Id).Update(orm.Params{
					"TeamId":result.Id,
					"GroupId":0,
				})
				if err!= nil{
					c.JSON(500, gin.H{
						"status": 500,
						"msg":    "系统错误,请刷新重试",
					})
					return
				}
				c.JSON(200, gin.H{
					"status": 200,
					"msg":"修改成功",
				})
				return
			}
		}
	}*/
	c.JSON(200, gin.H{
		"status": 200,
		"msg":"修改成功",
	})
	return
}
//插入猎头信息
func InsertAccountHeadhunters(c*gin.Context){
	var param model.InsertAccountApplyJob
	if err:=c.Bind(&param);err!=nil{
		c.JSON(500,gin.H{
			"status":500,
			"msg":"参数格式不正确",
		})
		return
	}
	total,e:=orm.NewOrm().QueryTable("account_headhunters").Filter("phone",param.Phone).Count()
	if e!=nil{
		c.JSON(500, gin.H{
			"status": 500,
			"msg":    "系统出错了,请刷新",
		})
		return
	}
	if total>0{
		c.JSON(200, gin.H{
			"status": 200,
			"msg":    "该手机号已被注册",
		})
		return
	}
	o:=orm.NewOrm()
	er:=o.Begin()
	if er!=nil{
		c.JSON(500, gin.H{
			"status": 500,
			"msg":    "系统出错了,请刷新",
		})
		return
	}
	accountheadhunters:= new(model.AccountHeadhunters)
	accountheadhunters.Phone =param.Phone
	accountheadhunters.PassWord=param.PassWord
	accountheadhunters.UserName =param.UserName
	accountheadhunters.Sex =param.Sex
	accountheadhunters.Age=param.Age
	accountheadhunters.Fphone=param.Fphone
	accountheadhunters.Fid=param.Fid
	accountheadhunters.Wxopenid=param.Wxopenid
	accountheadhunters.Time=param.Time
	num,err:=o.Insert(accountheadhunters)
	if err!= nil{
		c.JSON(500, gin.H{
			"status": 500,
			"msg":    "系统错误,请刷新重试",
		})
		return
	}
	o.Commit()
	c.JSON(200, gin.H{
		"status": 200,
		"msg":"添加成功",
		"thisid":num,
	})
	return
}
func LoginAccount(c*gin.Context){
	var param model.TypeRegister
	if err:=c.Bind(&param);err!=nil{
		c.JSON(400,gin.H{
			"status":400,
			"msg":"参数格式不正确",
		})
		return
	}
	if param.Atype==1{
		total,err:=orm.NewOrm().QueryTable("account_headhunters").Filter("phone",param.Phone).Filter("pass_word",param.Password).Count()
		if err!=nil{
			c.JSON(500, gin.H{
				"status": 500,
				"msg":    "系统出错了,请刷新",
			})
			return
		}
		if total>0{
			var result model.AccountHeadhunters
			err=orm.NewOrm().QueryTable("account_headhunters").Filter("phone",param.Phone).One(&result,"Id")
			if err!=nil{
				c.JSON(500, gin.H{
					"status": 500,
					"msg":    "系统出错了,请刷新",
				})
				return
			}
			c.JSON(200, gin.H{
				"status": 200,
				"msg":    1,
				"thisid":result.Id,
			})
			return
		}
	}else if param.Atype==0{
		num,e:=orm.NewOrm().QueryTable("account_apply_job").Filter("phone",param.Phone).Filter("pass_word",param.Password).Count()
		if e!=nil{
			c.JSON(500, gin.H{
				"status": 500,
				"msg":    "系统出错了,请刷新",
			})
			return
		}
		if num>0{
			var msg model.AccountApplyJob
			err:=orm.NewOrm().QueryTable("account_apply_job").Filter("phone",param.Phone).One(&msg,"Id")
			if err!=nil{
				c.JSON(500, gin.H{
					"status": 500,
					"msg":    "系统出错了,请刷新",
				})
				return
			}
			c.JSON(200, gin.H{
				"status": 200,
				"msg":    1,
				"thisid": msg.Id,
			})
			return
		}
	}
	c.JSON(403, gin.H{
			"status": 403,
			"msg":    2,
		})
	   return
}
func UploadAccountMsg (c*gin.Context){
	var param model.UploadAccountMsg
	if err:=c.Bind(&param);err!=nil{
		c.JSON(400,gin.H{
			"status":400,
			"msg":"参数格式不正确",
		})
		return
	}
	if param.Atype==0{
		o:=orm.NewOrm()
		num,err:=o.QueryTable("account_apply_job").Filter("id",param.Id).Update(orm.Params{
			"PicUrl":param.PicUrl,
			"UserName":param.UserName,
			"Sex":param.Sex,
			"Age":param.Age,
	})
	if num==0 {
		c.JSON(500, gin.H{
			"status": 500,
			"msg":    "系统错误,请刷新重试",
		})
		return
	}
	if err!= nil{
			c.JSON(500, gin.H{
				"status": 500,
				"msg":    "系统错误,请刷新重试",
			})
			return
		}
	}else if param.Atype==1{
		o:=orm.NewOrm()
		_,err:=o.QueryTable("account_headhunters").Filter("id",param.Id).Update(orm.Params{
			"PicUrl":param.PicUrl,
			"UserName":param.UserName,
			"Sex":param.Sex,
			"Age":param.Age,
		})
		if err!= nil{
			c.JSON(500, gin.H{
				"status": 500,
				"msg":    "系统错误,请刷新重试",
			})
			return
		}
	}else {
		c.JSON(403, gin.H{
			"status": 403,
			"msg":    "没有该用户类型",
		})
		return
	}
	c.JSON(200, gin.H{
		"status": 200,
		"msg":    "完善信息成功",
	})
	return
}
func ForgetPwd(c *gin.Context){
	var param model.ForgetPwd
	if err:=c.Bind(&param);err!=nil{
		c.JSON(500,gin.H{
			"status":500,
			"msg":"参数格式不正确",
		})
		return
	}
	if param.Atype==0{
		num,er:=orm.NewOrm().QueryTable("account_apply_job").Filter("phone",param.Phone).Count()
		if er!=nil{
			c.JSON(500,gin.H{
				"status":500,
				"msg":"系统错误请刷新",
			})
			return
		}
		if num==0{
			c.JSON(403,gin.H{
				"status":403,
				"msg":"没有找到绑定该手机号的用户",
			})
			return
		}
	}else if param.Atype==1 {
		num,er:=orm.NewOrm().QueryTable("account_headhunters").Filter("phone",param.Phone).Count()
		if er!=nil{
			c.JSON(500,gin.H{
				"status":500,
				"msg":"系统错误请刷新",
			})
			return
		}
		if num==0{
			c.JSON(403,gin.H{
				"status":403,
				"msg":"没有找到绑定该手机号的用户",
			})
			return
		}
	}else {
		c.JSON(403,gin.H{
			"status":403,
			"msg":"没有该类型的用户",
		})
		return
	}
	c.JSON(200,gin.H{
		"status":200,
		"msg":1,
	})
	return
}

func UpdatePwd(c*gin.Context){
	var param model.UpdatePwd
	if err:=c.Bind(&param);err!=nil{
		c.JSON(403,gin.H{
			"status":403,
			"msg":"参数格式不正确",
		})
		return
	}
	if param.Atype==0{
		o:=orm.NewOrm()
		_,err:=o.QueryTable("account_apply_job").Filter("phone",param.Phone).Update(orm.Params{
			"PassWord":param.NewPwd,
		})
		if err!=nil{
			c.JSON(500,gin.H{
				"status":500,
				"msg":"系统错误请刷新",
			})
			return
		}
	}else if param.Atype==1{
		o:=orm.NewOrm()
		_,err:=o.QueryTable("account_headhunters").Filter("phone",param.Phone).Update(orm.Params{
			"PassWord":param.NewPwd,
		})
		if err!=nil{
			c.JSON(500,gin.H{
				"status":500,
				"msg":"系统错误请刷新",
			})
			return
		}
	}else {
		c.JSON(403,gin.H{
			"status":403,
			"msg":"没有该类型的用户",
		})
		return
	}
	c.JSON(200,gin.H{
		"status":200,
		"msg":1,
	})
	return
}
//猎头的推荐记录
func GetAccountHeadhuntersEnrolLog (c*gin.Context){
	var param model.TypeId
	if err:=c.Bind(&param);err!=nil {
		c.JSON(400, gin.H{
			"status": 400,
			"msg":    "参数格式不正确",
		})
		return
	}
	var phone model.AccountHeadhunters
	err:=orm.NewOrm().QueryTable("account_headhunters").Filter("id",param.Id).One(&phone,"phone")
	if err!=nil {
		c.JSON(500, gin.H{
			"status": 500,
			"msg":    "系统错误请刷新重试",
		})
		return
	}
	if phone.Phone==""{
		c.JSON(403, gin.H{
			"status": 403,
			"msg":    "没有该用户",
		})
		return
	}
	var msg []model.GetAJJLogMsg
	num,err:=orm.NewOrm().Raw("SELECT position.commission,position.position,position.company,position.area,position.wages,resume.user_name,position.work_time,position.edueational,resume_log.`status`,resume_log.date,resume_log.remark FROM position,resume,resume_log WHERE resume_log.fphone IN (?,?) AND resume_log.account_id=resume.account_id AND resume_log.position_id=position.id ORDER BY resume_log.id DESC",param.Id,phone.Phone).QueryRows(&msg)
	if err!=nil {
		c.JSON(500,gin.H{
			"status":500,
			"msg":"系统错请刷新重试",
		})
		return
	}
	var result []model.GetAJJLogMsg
	total,err:=orm.NewOrm().Raw("SELECT position.commission,position.position,position.company,position.area,position.wages,resume.user_name,position.work_time,position.edueational,enrol_log.`status`,enrol_log.date,enrol_log.remark FROM position,resume,enrol_log WHERE enrol_log.fphone IN (?,?) AND enrol_log.account_id=resume.account_id AND enrol_log.position_id=position.id  ORDER BY enrol_log.id DESC",param.Id,phone.Phone).QueryRows(&result)
	if err!=nil {
		c.JSON(500,gin.H{
			"status":500,
			"msg":"系统错请刷新重试",
		})
		return
	}
	var aajlog model.AAJLog
	for range msg{
		aajlog.ResumeLog=msg
	}
	for range result{
		aajlog.EnrloLog=result
	}

	c.JSON(200,gin.H{
		"status":200,
		"data":aajlog,
		"total":num+total,
	})
	return
}
